package com.sgb.goods.viewmodel;

import androidx.databinding.ObservableBoolean;

import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.api.GoodsCallBack;
import com.sgb.goods.api.GoodsNet;
import com.sgb.goods.entity.TradingContractEntity;
import com.sgb.goods.utils.base.adapter.BaseCommonAdapter;
import com.sgb.goods.utils.base.viewmodel.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;

import java.util.ArrayList;
import java.util.List;

/***
 * 采购商要求
 * @author cyj
 */
public class TradingContractViewModel extends BaseViewModel {

    public BaseCommonAdapter<TradingContractEntity.RequestItem> adapter;
    public BaseCommonAdapter<String[]> serverAdapter;
    public ObservableBoolean hasRequestItemData = new ObservableBoolean();

    public TradingContractViewModel() {
        adapter = new BaseCommonAdapter<>(R.layout.goods_item_trading_contract, BR.viewModel);
        serverAdapter = new BaseCommonAdapter<>(R.layout.goods_item_trading_server, BR.viewModel);
    }

    /** 获取采购商要求 */
    public void getTradingContractContent(String orderId) {
        GoodsNet.getInstance().getGoodsApi().getOtherInfo(orderId).enqueue(new GoodsCallBack<BaseEntity<TradingContractEntity>>() {
            @Override
            public void onSuccess(BaseEntity<TradingContractEntity> data) {
                // 解析出其他要求相关数据
                adapter.setNewData(data.getData().other);
                hasRequestItemData.set(null != data.getData().other && data.getData().other.size() > 0);

                // 解析出订单服务相关数据
                TradingContractEntity tradingContractEntity = data.getData();
                List<String[]> serverData = new ArrayList<>();
                serverData.add(showLoadService(tradingContractEntity).split("\\|"));
                serverData.add(showUnLoadService(tradingContractEntity).split("\\|"));
                serverData.add(showInstallationService(tradingContractEntity).split("\\|"));
                serverAdapter.setNewData(serverData);
            }
        });
    }

    /** 安装服务 默认 0 无 1 卖方承担 2 买方承担 */
    private String showInstallationService(TradingContractEntity tradingContractEntity) {
        if(1 == tradingContractEntity.installService) {
            return "安装服务|" + "卖方承担";
        } else if(2 == tradingContractEntity.installService) {
            return "安装服务|" + "买方承担";
        } else {
            return "安装服务|" + "/";
        }
    }

    /** 卸车服务 默认不选中为0 ，1买方承担 2 卖方承担 3 共同承担 4 待定 */
    private String showUnLoadService(TradingContractEntity tradingContractEntity) {
        if(1 == tradingContractEntity.unloading) {
            return "卸车服务|" + "买方承担";
        } else if(2 == tradingContractEntity.unloading) {
            return "卸车服务|" + "卖方承担";
        }  else if(3 == tradingContractEntity.unloading) {
            return "卸车服务|" + "共同承担";
        }  else if(4 == tradingContractEntity.unloading) {
            return "卸车服务|" + "待定";
        } else {
            return "卸车服务|" + "/";
        }
    }

    /** 装车服务 默认不选中为0 ，1买方承担 2 卖方承担 */
    private String showLoadService(TradingContractEntity tradingContractEntity) {
        if(1 == tradingContractEntity.loading) {
            return "装车服务|" + "买方承担";
        } else if(2 == tradingContractEntity.loading) {
            return "装车服务|" + "卖方承担";
        } else {
            return "装车服务|" + "/";
        }
    }

    /** 收货方式 0 默认 1 卖方送达验收 2 买方提货验收  (1.5.0这个收货方式要求隐藏，后续会有) */
    private String showReceiptWay(TradingContractEntity tradingContractEntity) {
        if(1 == tradingContractEntity.receivingMethod) {
            return "收货方式|" + "卖方送达验收";
        } else if(2 == tradingContractEntity.receivingMethod) {
            return "收货方式|" + "买方提货验收";
        } else {
            return "";
        }
    }
}
